import java.util.Random;

class Bank {
    private int totalAmount;

    public Bank(int amount) {
        totalAmount = amount;
    }

    public synchronized boolean borrowMoney(int amount) {
        if (totalAmount >= amount) {
            totalAmount -= amount;
            return true;
        } else {
            return false;
        }
    }

    public synchronized void returnMoney(int amount) {
        totalAmount += amount;
        notifyAll(); // Notify waiting threads
    }

    public synchronized int getTotalAmount() {
        return totalAmount;
    }
}

class ProjectTeam extends Thread {
    private String name;
    private int requiredAmount;
    private Bank bank;

    public ProjectTeam(String name, int requiredAmount, Bank bank) {
        this.name = name;
        this.requiredAmount = requiredAmount;
        this.bank = bank;
    }

    public void run() {
        int borrowedAmount = 0;
        Random rand = new Random();

        while (borrowedAmount < requiredAmount) {
            int toBorrow = rand.nextInt(requiredAmount - borrowedAmount) + 1;
            if (bank.borrowMoney(toBorrow)) {
                borrowedAmount += toBorrow;
                System.out.println(name + " borrowed " + toBorrow + " million. Total borrowed: " + borrowedAmount + " million");
            } else {
                try {
                    System.out.println(name + " is waiting for more funds...");
                    wait(); // Wait until more funds are available
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println(name + " project completed. Returning borrowed funds.");
        bank.returnMoney(requiredAmount);
        System.out.println(name + " returned " + requiredAmount + " million. Total funds available in bank: " + bank.getTotalAmount() + " million");
    }
}

public class Main {
    public static void main(String[] args) {
        Bank bank = new Bank(1000); // Starting with 1000 million available funds

        ProjectTeam teamA = new ProjectTeam("A", 500, bank);
        ProjectTeam teamB = new ProjectTeam("B", 500, bank);
        ProjectTeam teamC = new ProjectTeam("C", 500, bank);

        teamA.start();
        teamB.start();
        teamC.start();
    }
}